package cn.iocoder.yudao.module.member.dal.mysql.company;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.member.dal.dataobject.company.CompanyDO;
import cn.iocoder.yudao.module.member.dal.dataobject.company.CompanyUserDO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.member.controller.admin.company.vo.*;
import org.apache.ibatis.annotations.Param;

/**
 * 企业管理 Mapper
 *
 * @author 周枫皓
 */
@Mapper
public interface CompanyMapper extends BaseMapperX<CompanyDO> {

    default PageResult<CompanyDO> selectPage(CompanyPageReqVO reqVO) {
        List<String>  authorizedStatuslist=new ArrayList<>();
        if(reqVO.getAuthorizedStatus()!=null){
            authorizedStatuslist.add(reqVO.getAuthorizedStatus());
        }
        if(reqVO.getAuthorizedStatus1()!=null){
            authorizedStatuslist.add(reqVO.getAuthorizedStatus1());
        }
        Boolean flag=reqVO.getFlag()!=null&&reqVO.getFlag().equals("1");
        return pageQuery(reqVO, new LambdaQueryWrapperX<CompanyDO>()
                .eqIfPresent(CompanyDO::getPhoneNumber, reqVO.getPhoneNumber())
                .eqIfPresent(CompanyDO::getStatus, reqVO.getStatus())
                .eqIfPresent(CompanyDO::getIsHot, reqVO.getIsHot())
                .eqIfPresent(CompanyDO::getIsNew, reqVO.getIsNew())
                .likeIfPresent(CompanyDO::getCompanyName, reqVO.getCompanyName())
                .likeIfPresent(CompanyDO::getTel, reqVO.getTel())
                .likeIfPresent(CompanyDO::getCompanyAddress, reqVO.getCompanyAddress())
                .inIfPresent(CompanyDO::getIndustry, reqVO.getIndustry())
//                .likeIfPresent(CompanyDO::getIndustry, reqVO.getIndustry()).likeIfPresent(CompanyDO::getIndustry, reqVO.getIndustry())
                .inIfPresent(CompanyDO::getAuthorizedStatus,authorizedStatuslist)
                .betweenIfPresent(CompanyDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .isNotNull(flag,CompanyDO::getIntroduction)
                 .notInIfPresent( CompanyDO::getId,reqVO.getIds())
                .orderBy(CompanyDO.class, PageParam.DEFAULT_SORTING_FIELDS)
        );
    }

    default List<CompanyDO> selectList(CompanyExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<CompanyDO>()
                .eqIfPresent(CompanyDO::getPhoneNumber, reqVO.getPhoneNumber())
                .likeIfPresent(CompanyDO::getTel, reqVO.getTel())
                .likeIfPresent(CompanyDO::getCompanyName, reqVO.getCompanyName())
                .betweenIfPresent(CompanyDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(CompanyDO::getId)
                .orderBy(CompanyDO.class, PageParam.DEFAULT_SORTING_FIELDS)
        );
    }

    //TODO 手写一个count方法返回总数据量 待优化SQL语句-不一定需要连表查询进行count
    Integer countCompanyUser(@Param("request") CompanyUserPageReqVO request);

    List<CompanyUserDO> getCompanyUserPage(@Param("request")CompanyUserPageReqVO request);

    List<CompanyDO>  getHotCompanyPage(@Param("request")CompanyPageReqVO pageReqVO);

    List<CompanyDO>  getCompanyByIsHot( );

    List<CompanyDO>  getCompanyAllByIds(@Param("ids") List<Long> ids);

    Integer getCompanyRegistCount(@Param("startDate") String startDate,@Param("endDate")String endDate);

}
